package com.pureut.equipment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.equipment.domain.PatrolItem;
import com.pureut.equipment.domain.dto.PatrolItemDto;
import com.pureut.equipment.domain.dto.PatrolItemPlanDto;
import com.pureut.equipment.domain.vo.PatrolItemVo;
import com.pureut.equipment.mapper.PatrolItemMapper;
import com.pureut.equipment.mapper.PatrolItemPlanMapper;
import com.pureut.equipment.service.PatrolItemService;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.api.RemoteCodeService;
import com.sale.system.api.domain.SysDictData;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 *
 * Author:巡检项目实现层
 * Date:2023/04/26 9:46
 * @DESC
 */
@Service
public class PatrolItemServiceImpl extends ServiceImpl<PatrolItemMapper, PatrolItem> implements PatrolItemService {

    /**
     * 巡检项目mapper
     */
    @Resource
    PatrolItemMapper patrolItemMapper;

    /**
     * 巡检计划mapper
     */
    @Resource
    PatrolItemPlanMapper patrolItemPlanMapper;

    @DubboReference
    RemoteCodeService remoteCodeService;

    /**
     * 巡检项目列表
     *
     * @param patrolItemVo
     * @return
     */
    @Override
    public TableDataInfo<PatrolItemDto> getList(PatrolItemVo patrolItemVo, PageQuery pageQuery) {
        //获取检验类型字典
        List<SysDictData> typeArray = DictUtils.getDictCache("inspection_item_inspection_type");
        Map<String, String> typeMap = typeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        Page<PatrolItemDto> list = patrolItemMapper.getList(patrolItemVo, pageQuery.build());
        List<PatrolItemDto> records = list.getRecords();
        for (PatrolItemDto entity : records) {
            //检验类型
            entity.setInspectionTypeDict(typeMap.get(entity.getInspectionType()));
        }

        return TableDataInfo.build(list);
    }

    /**
     * 新增
     *
     * @param patrolItemVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(PatrolItemVo patrolItemVo) throws Exception {
        PatrolItem patrolItem = new PatrolItem();
        String authorityCoding = remoteCodeService.authorityCoding("device:inspection:project:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        //当检验类型为范围值时候
        if (patrolItemVo.getInspectionType() == 3) {
            String[] split = patrolItemVo.getDecisionResult().split(",");
            //下限不能大于上限值
            if (Double.parseDouble(split[0]) > Double.parseDouble(split[1])) {
                throw new GlobalException("下限值不能大于上限值");
            }
        }
        patrolItem.setPatrolItemCode(authorityCoding)
            .setPatrolItemName(patrolItemVo.getPatrolItemName())
            .setWorkingHour(patrolItemVo.getWorkingHour())
            .setPatrolItemContent(patrolItemVo.getPatrolItemContent())
            .setInspectionStandard(patrolItemVo.getInspectionStandard())
            .setInspectionType(patrolItemVo.getInspectionType())
            .setDecisionResult(patrolItemVo.getDecisionResult())
            .setRemark(patrolItemVo.getRemark())
            .setCreateTime(new Date())
            .setCreateBy(LoginHelper.getUsername())
            .setDeptId(LoginHelper.getDeptId());
        return save(patrolItem);
    }

    /**
     * 修改
     *
     * @param patrolItemVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePatrolItem(PatrolItemVo patrolItemVo) {
        PatrolItem patrolItem = getById(patrolItemVo.getId());
        patrolItem.setPatrolItemName(patrolItemVo.getPatrolItemName())
            .setWorkingHour(patrolItemVo.getWorkingHour())
            .setPatrolItemContent(patrolItemVo.getPatrolItemContent())
            .setInspectionStandard(patrolItemVo.getInspectionStandard())
            .setInspectionType(patrolItemVo.getInspectionType())
            .setDecisionResult(patrolItemVo.getDecisionResult())
            .setRemark(patrolItemVo.getRemark());
        return updateById(patrolItem);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deletePatrolItem(Long id) {
        PatrolItem byId = getById(id);
        // 该项目已关联巡检计划，无法删除
        List<PatrolItemPlanDto> patrolItemPlanDtos = patrolItemPlanMapper.selectPatrolItem(id);
        if (patrolItemPlanDtos.size() > 0) {
            throw new GlobalException("该项目已经关联巡检计划，无法删除");
        }
        return removeById(byId);
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public PatrolItemDto queryPatrolItem(Long id) {


        return patrolItemMapper.getDataById(id);
    }
}
